package com.document;

import cn.hutool.core.codec.Base64;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.converter.WordToHtmlUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;


import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;

/**
 * @ClassName : WordToHtml
 * @Description ：
 * @Author : zxy
 * @Date : 2023/10/26 11:38
 * @Version : 1.0
 */
public class WordToHtml {


    public static void main(String[] args) throws Exception {

        String f = "D:\\ruoyi\\uploadPath\\upload\\2023\\11\\14\\template_20231114110414A001\\软件设计说明书.docx";
        String htmlp = "D:\\ruoyi\\uploadPath\\upload\\2023\\11\\14\\template_20231114110414A001\\软件设计说明书.html";
        convertDocxFileToHtml(f,htmlp);
    }

    public static void convertDocxFileToHtml(String fielPath, String htmlPath) throws Exception {
        convertDocxFileToHtml(new File(fielPath), htmlPath);
    }

    public static void convertDocxFileToHtml(File fielPath, String htmlPath) throws Exception {
        convertDocxFileToHtml(new FileInputStream(fielPath), htmlPath);
    }

    /**
     * 将 docx 转成 html
     *
     * @throws Exception
     */
    public static void convertDocxFileToHtml(InputStream fielPath, String htmlPath) throws Exception {
        //创建操作word的对象
        XWPFDocument document = new XWPFDocument(fielPath);
        XHTMLOptions options = XHTMLOptions.create();
        options.setIgnoreStylesIfUnused(false);
        options.setFragment(true);
        //图片用base64转化
        options.setImageManager(new Base64ImgManager());
        OutputStream stream = new FileOutputStream(htmlPath);
        //转化成HTML
        XHTMLConverter.getInstance().convert(document, stream, options);
    }

    public static void convertDocFileToHtml(String fielPath, String htmlPath) throws Exception {
        convertDocFileToHtml(new File(fielPath), htmlPath);
    }

    public static void convertDocFileToHtml(File fielPath, String htmlPath) throws Exception {
        convertDocFileToHtml(new FileInputStream(fielPath), htmlPath);
    }

    public static void convertDocFileToHtml(InputStream fielPath, String htmlPath) throws Exception {
        //ps:当inputStream!=null，而生成wordDocument报错，请检查文档是否用office word保存的
        HWPFDocument wordDocument = (HWPFDocument) WordToHtmlUtils.loadDoc(fielPath);
        WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
                DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
        //将图片转成base64的格式
        PicturesManager pictureRunMapper = (bytes, pictureType, s, v, v1) -> Base64ImgManager.EMBED_IMG_SRC_PREFIX + Base64.encode(bytes);
        wordToHtmlConverter.setPicturesManager(pictureRunMapper);
        //解析word文档
        wordToHtmlConverter.processDocument(wordDocument);
        Document htmlDocument = wordToHtmlConverter.getDocument();
        DOMSource domSource = new DOMSource(htmlDocument);
        OutputStreamWriter outputStream = new OutputStreamWriter(new FileOutputStream(htmlPath), "utf-8");

        StreamResult streamResult = new StreamResult(outputStream);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer serializer = factory.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.METHOD, "html");
        serializer.transform(domSource, streamResult);
    }

}
